//
//  FunctionCell.m
//  iOS实战
//
//  Created by clz on 2019/10/26.
//  Copyright © 2019 clz. All rights reserved.
//

#import "FunctionCell.h"
#import <Masonry.h>
#import "FunctionItemCell.h"
#import "Commont.h"

@interface FunctionCell()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property(nonatomic,strong)UICollectionView *collectionView;

@end

@implementation FunctionCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.functionCellVM = [[FunctionCellVM alloc]init];
        [self.functionCellVM addObserver:self forKeyPath:@"functionList" options:NSKeyValueObservingOptionNew context:nil];
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
        CGFloat width = 60;
        layout.minimumLineSpacing = ([UIScreen mainScreen].bounds.size.width - 4 * width - 40)/3.0;
        layout.itemSize = CGSizeMake(width, 92.5);
        layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        
        self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        self.collectionView.backgroundColor = [UIColor whiteColor];
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        [self.collectionView registerClass:[FunctionItemCell class] forCellWithReuseIdentifier:@"FunctionItemCell"];
        
        [self.contentView addSubview:self.collectionView];
        
        
        UIView *line = [[UIView alloc]init];
        line.backgroundColor = RGBHex(0xECEFF1);
        [self.contentView addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(20);
            make.right.equalTo(self.contentView).offset(-20);
            make.bottom.equalTo(self.contentView);
            make.height.mas_equalTo(0.5);
        }];
        
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(20);
            make.top.equalTo(self.contentView);
            make.right.equalTo(self.contentView).offset(-20);
            make.height.mas_equalTo(93);
            make.bottom.equalTo(line.mas_top).offset(-19);
        }];
    }
    return self;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.functionCellVM.functionList.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    FunctionItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FunctionItemCell" forIndexPath:indexPath];
    
    [cell.functionItemCellVM setModel:self.functionCellVM.functionList[indexPath.row]];
    
    return cell;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    
    id value = change[NSKeyValueChangeNewKey];
    
    if ([keyPath isEqualToString:@"functionList"]) {
        
        [self.collectionView reloadData];
        
    }
}


- (void)dealloc{
    [self.functionCellVM removeObserver:self forKeyPath:@"functionList"];
}






@end
